﻿using System.Collections.Generic;
using System.Modules.SharePoint.Contracts.Enums;
using Microsoft.SharePoint;
using System.Modules.Contracts.DTO;

namespace System.Modules.SharePoint.Contracts.DTO
{
    /// <summary>
    /// SP Site Data Query
    /// use this class to build SPSiteDataQuery
    /// </summary>
    public class SPSiteDataQueryBuilder : Initiator
    {
        #region Properties
        /// <summary>
        /// Gets or sets the inner XML that specifies which lists to include in the query
        /// </summary>
        public SPSiteDataQueryLists Lists { get; set; }

        /// <summary>
        /// Gets or sets the inner XML that defines the query
        /// </summary>
        public SPQueryBuilder Query { get; set; }

        /// <summary>
        /// Gets or sets a value that indicates the throttling mode to be applied to the query
        /// </summary>
        public SPQueryThrottleOption? QueryThrottleMode { get; set; }

        /// <summary>
        /// Gets or sets a limit for the number of items in the query that are returned per page.
        /// </summary>
        public uint? RowLimit { get; set; }

        /// <summary>
        /// Gets or sets the inner XML that specifies the view fields used in the query
        /// </summary>
        public List<SPQueryViewField> ViewFields { get; set; }

        /// <summary>
        /// Gets or sets the inner XML that specifies which Web sites to include in the query. as specified by the Scope attribute on the Webs tag in the query. By default, the query considers the current Web site, that is, the Web site from which the 
        /// </summary>
        public SPQueryWebsScope Webs { get; set; }
        #endregion

        #region Implicit Operators
        /// <summary>
        /// Implicit conversion operator to string
        /// </summary>
        /// <param name="value">The SP Query Value</param>
        /// <returns>A string representation of the object</returns>
        public static implicit operator string(SPSiteDataQueryBuilder value)
        {
            return value == null ? string.Empty : value.ToString();
        }
        #endregion

        #region Methods
        /// <summary>
        /// Converts the current object into SPSiteDataQuery
        /// </summary>
        /// <returns></returns>
        public SPSiteDataQuery ToQuery()
        {
            var value = new SPSiteDataQuery();
            value.Lists = Lists;
            value.Query = Query;
            value.Webs = Webs.ToString();
            if (RowLimit.HasValue) value.RowLimit = RowLimit.Value;
            if (QueryThrottleMode.HasValue) value.QueryThrottleMode = QueryThrottleMode.Value;
            return value;
        }
        public override string ToString()
        {
            return base.ToString();
        }
        #endregion
    }
}
